using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Content
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Content : SqlServerDAO
    {
        #region Content Private Fields

		private Int32 _id;
		private String _contentText;
		private DateTime _lastUpdate;

        #endregion

        #region Content Constractors

		public Content(){}

		public Content
		(
			Int32 id,
			String contentText,
			DateTime lastUpdate

		)
		{
			this._id = id;
			this._contentText = contentText;
			this._lastUpdate = lastUpdate;
		}

        #endregion

        #region Content Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String ContentText
		{
			get{ return this._contentText; }
			set{ this._contentText = value; }
		}

		public DateTime LastUpdate
		{
			get{ return this._lastUpdate; }
			set{ this._lastUpdate = value; }
		}


        #endregion

        #region Content Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewContent
        (
			String contentText,
			DateTime lastUpdate

        )
        {
            //creating an empty Content object
            Content content = new Content();
            content.ContentText = contentText;
			content.LastUpdate = lastUpdate;

            return Content.CreateNewContent(content);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateContent
        (
			Int32 id,
			String contentText,
			DateTime lastUpdate

        )
        {
            Content content = Content.GetContentById(id);
            content.ContentText = contentText;
			content.LastUpdate = lastUpdate;

            return Content.UpdateContent(content);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteContent
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Content"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_DELETECONTENT);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteContentList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Content"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_DELETECONTENTLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Content Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewContent(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateContent(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Content> GetAllContent()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_GETALLCONTENT);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContentCollectionFromReader);
            CustomCollection<Content> objCollection = ((CustomCollection<Content>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Content> GetAllContentPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_GETALLCONTENTPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContentCollectionFromReader);
            CustomCollection<Content> objCollection = ((CustomCollection<Content>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllContentPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_GETALLCONTENTPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Content GetContentById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Content"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_GETCONTENTBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateContentCollectionFromReader);
            CustomCollection<Content> objCollection = ((CustomCollection<Content>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Content)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewContent(Content newContent)
        {
            // validate Parameters
            if (newContent == null)
                throw (new ArgumentNullException("newContent in Content"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@ContentText", SqlDbType.NText, 0, ParameterDirection.Input, newContent.ContentText);
			AddParameterToSqlCmd(sqlCmd, "@LastUpdate", SqlDbType.DateTime, 0, ParameterDirection.Input, newContent.LastUpdate);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_CREATENEWCONTENT);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateContent(Content newContent)
        {
            // validate Parameters
            if (newContent == null)
                throw (new ArgumentNullException("newContent"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newContent.Id);
            AddParameterToSqlCmd(sqlCmd, "@ContentText", SqlDbType.NText, 0, ParameterDirection.Input, newContent.ContentText);
			AddParameterToSqlCmd(sqlCmd, "@LastUpdate", SqlDbType.DateTime, 0, ParameterDirection.Input, newContent.LastUpdate);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_CONTENT_UPDATECONTENT);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Content Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Content custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Content information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateContentCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Content collection
            CustomCollection<Content> colContent = new CustomCollection<Content>();

            //Iterating thriugh the data reader, to generate Content collection.

            //getting the Content collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Content constructor parameters from the current instance of data reader fields.
                Content newContent = new Content
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["content_text"] == System.DBNull.Value ? String.Empty : (String)returnData["content_text"],
					returnData["last_update"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["last_update"]

                );
                //adding the Content to the collection
                colContent.Add(newContent);
            }

            //returns the collection of Content 
            return (colContent);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_CONTENT_CREATENEWCONTENT = "spr_Content_CreateNewContent";
        internal const string SPR_CONTENT_UPDATECONTENT = "spr_Content_UpdateContent";
        internal const string SPR_CONTENT_DELETECONTENT = "spr_Content_DeleteContent";
        internal const string SPR_CONTENT_DELETECONTENTLIST = "spr_Content_DeleteContentList";
        internal const string SPR_CONTENT_GETCONTENTBYID = "spr_Content_GetContentById";
        internal const string SPR_CONTENT_GETALLCONTENT = "spr_Content_GetAllContent";
        internal const string SPR_CONTENT_GETALLCONTENTPAGED = "spr_Content_GetAllContentPaged";
        internal const string SPR_CONTENT_GETALLCONTENTPAGEDCOUNT = "spr_Content_GetAllContentPagedCount";

		//Foreign key contants
		
        #endregion
    }
}
